package gui;

import java.awt.BorderLayout;
import controlLayer.ProductCtr;
import modelLayer.Product;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Window.Type;

import modelLayer.Product;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;

public class EditProductGui extends JFrame {

	private JPanel contentPane;
	private JTextField newCarModel;
	private JTextField newCarEngineType;
	private JTextField newCarPartName;
	private JTextField newPrice;
	private JTextField newStockQuantity;
	private JTextField oldCarModel;
	private JTextField oldEngineType;
	private JTextField oldPartName;
	private JTextField oldPrice;
	private JTextField oldStockQuantity;
	private ProductCtr productCtr = new ProductCtr();
	private JTextField oldCarBrand;
	private Product product;

	/**
	 * Create the frame.
	 */
	public EditProductGui(final Product product) {
		this.product = product;
		setAlwaysOnTop(true);
		setTitle("Edit Product ");
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 279);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[][grow][grow]", "[][][][grow][][][][][][][]"));
		
		JLabel lblAddProduct = new JLabel("Edit Product");
		contentPane.add(lblAddProduct, "cell 0 0 3 1,alignx center");
		
		JLabel lblOldValues = new JLabel("Old Values");
		contentPane.add(lblOldValues, "cell 1 1,alignx center");
		
		JLabel lblNewValues = new JLabel("New Values");
		contentPane.add(lblNewValues, "cell 2 1,alignx center");
		
		JSeparator separator = new JSeparator();
		contentPane.add(separator, "cell 0 2 3 1,growx");
		
		JLabel lblBrand = new JLabel("Car Brand:");
		contentPane.add(lblBrand, "cell 0 3,alignx trailing");
		
		oldCarBrand = new JTextField();
		oldCarBrand.setEnabled(false);
		contentPane.add(oldCarBrand, "cell 1 3,growx");
		oldCarBrand.setColumns(10);
		
		final JComboBox newCarBrand = new JComboBox();
		newCarBrand.setEditable(true);
		newCarBrand.setModel(new DefaultComboBoxModel(new String[] {"Alfa Romeo", "Audi", "BMW", "Chevrolet", "Chrysler", "Citro\u00EBn", "Dacia", "Daewoo", "Daihatsu", "Dodge", "Fiat", "Ford", "Honda", "Hyundai", "Iveco", "Jaguar", "Jeep", "Kia", "Lancia", "Land Rover", "Lexus", "Mazda", "Mercedes", "Mini", "Mitsubishi", "Nissan", "Opel", "Peugeot", "Porsche", "Renault", "Rover", "Saab", "Seat", "Skoda", "Smart", "Subaru", "Suzuki", "Toyota", "Volkswagen", "Volvo"}));
		contentPane.add(newCarBrand, "cell 2 3,growx");
		
		JLabel lblModel = new JLabel("Model:");
		contentPane.add(lblModel, "cell 0 4,alignx trailing");
		
		oldCarModel = new JTextField();
		oldCarModel.setEnabled(false);
		contentPane.add(oldCarModel, "cell 1 4,growx");
		oldCarModel.setColumns(10);
		
		newCarModel = new JTextField();
		contentPane.add(newCarModel, "cell 2 4,growx");
		newCarModel.setColumns(10);
		
		JLabel lblEngineType = new JLabel("Engine type:");
		contentPane.add(lblEngineType, "cell 0 5,alignx trailing");
		
		oldEngineType = new JTextField();
		oldEngineType.setEnabled(false);
		contentPane.add(oldEngineType, "cell 1 5,growx");
		oldEngineType.setColumns(10);
		
		newCarEngineType = new JTextField();
		contentPane.add(newCarEngineType, "cell 2 5,growx");
		newCarEngineType.setColumns(10);
		
		JLabel lblPartName = new JLabel("Part name:");
		contentPane.add(lblPartName, "cell 0 6,alignx trailing");
		
		oldPartName = new JTextField();
		oldPartName.setEnabled(false);
		contentPane.add(oldPartName, "cell 1 6,growx");
		oldPartName.setColumns(10);
		
		newCarPartName = new JTextField();
		contentPane.add(newCarPartName, "cell 2 6,growx");
		newCarPartName.setColumns(10);
		
		JLabel lblPrice = new JLabel("Price:");
		contentPane.add(lblPrice, "cell 0 7,alignx trailing");
		
		oldPrice = new JTextField();
		oldPrice.setEnabled(false);
		contentPane.add(oldPrice, "cell 1 7,growx");
		oldPrice.setColumns(10);
		
		newPrice = new JTextField();
		contentPane.add(newPrice, "cell 2 7,growx");
		newPrice.setColumns(10);
		
		JLabel lblNumber = new JLabel("Quantity:");
		contentPane.add(lblNumber, "cell 0 8,alignx trailing");
		
		oldStockQuantity = new JTextField();
		oldStockQuantity.setEnabled(false);
		contentPane.add(oldStockQuantity, "cell 1 8,growx");
		oldStockQuantity.setColumns(10);
		
		newStockQuantity = new JTextField();
		contentPane.add(newStockQuantity, "cell 2 8,growx");
		newStockQuantity.setColumns(10);
		
		JSeparator separator_1 = new JSeparator();
		contentPane.add(separator_1, "cell 0 9 3 1,growx");
		
		JButton btnNewButton = new JButton("Update");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int productId = product.getProductId();
				String carBrand = (String) newCarBrand.getSelectedItem();
				String carModel = newCarModel.getText();
				String engineType = newCarEngineType.getText();
				String partName = newCarPartName.getText();
				double price = newPrice.getText().equals("") ? 0 : Double.parseDouble(newPrice.getText());
				int stockQuantity = newStockQuantity.getText().equals("") ? 0 : Integer.parseInt(newStockQuantity.getText());
				
				Product updateProduct = new Product(productId, carBrand, carModel, engineType, partName, price, stockQuantity);
				try {
					productCtr.updateProduct(getProduct(), updateProduct);
					
					setProduct(productCtr.searchProduct(updateProduct));
					refreshOldFields();
					MainGui.refreshTables();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		contentPane.add(btnNewButton, "cell 0 10,growx");
		
		JButton btnNewButton_1 = new JButton("Cancel");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete? The product can't be recovered.", "Delete confirmation" , JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
				{
					try {
						productCtr.deleteProduct(product);
						MainGui.refreshTables();
						dispose();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		contentPane.add(btnDelete, "cell 1 10");
		contentPane.add(btnNewButton_1, "cell 2 10");
		refreshOldFields();
	}
	private Product getProduct()
	{
		return this.product;
	}
	private void setProduct(Product product)
	{
		this.product = product;
	}
	private void refreshOldFields()
	{

		oldCarBrand.setText(product.getCarBrand());
		oldCarModel.setText(product.getCarModel());
		oldEngineType.setText(product.getEngineType());
		oldPartName.setText(product.getPartName());
		oldPrice.setText(""+product.getPrice());
		oldStockQuantity.setText(""+product.getStockQuantity());
	}
}
